#include "../base/array.h"
#include <assert.h>
#include <stdio.h>

#define TEST(case_num) \
	test##case_num

void TEST(1)() {
	struct array arr;
	size_t i = 0 ;
	array_create( &arr , 10 , sizeof(int) );
	 for( i = 0 ; i <10 ; ++i )
		array_push_back(&arr,&i);
	assert( array_get_usesize(&arr) == 10 );
	assert( array_get_capacity(&arr) == 10 );
	i = 1000;
	array_push_back( &arr , &i );
	assert( array_get_usesize(&arr) == 11 );
	assert( array_get_capacity(&arr) == 20 );
	
	array_expand(&arr,100);
	assert( array_get_usesize(&arr) == 100 );
	assert( array_get_capacity(&arr) == 100 );
	// clear the array 
	array_clear(&arr);
	assert( array_get_usesize(&arr) == 0 );
	assert( array_get_capacity(&arr) == 100);
	array_destroy(&arr);
}


void print_what_you_have( void* obj , int idx ) {
	printf("The object is %d , index is %d",*(int*)(obj),idx);
}

void TEST(2)() {
	struct array arr;
	array_create(&arr,10,sizeof(int));
	size_t i = 0 ; 
	// we gonna test the iterating machnism for array
	for( i = 0 ; i < 1000 ; ++i )
		array_push_back(&arr,&i);
	array_foreach(&arr,print_what_you_have,NULL);
	
	struct array arr_cl;
	array_clone(&arr,&arr_cl);
	array_foreach(&arr_cl,print_what_you_have,NULL);
	
	array_clear(&arr);
	array_destroy(&arr);
}


/*
 * Writing the test makes me clear how easy to lead fault by using the array
 * The array requires the use of pointer ,which makes the push_back have to 
 * pass in the pointer , this seems to be very easy to lead fault , since in
 * most cases , the user will just pass in the variable ,then the program will
 * recieve a SIGSEGV signal which tells you the error usage of the memory.
 */
 
void test() {
	TEST(1)();
	TEST(2)();
	printf("Test completely");
}




